<?php

namespace App\Http\Controllers\Brand;

use App\Events\ToJoin;
use App\Http\Controllers\BaseController;
use App\Models\FirmBrandMessage;
use App\Rules\Code;
use Illuminate\Http\Request;

class MessageController extends BaseController
{
    public function store(Request $request)
    {
        $param = $this->validate($request,[
            'type' => 'required|in:1,2,3',
            'brand_id' => 'required|integer',
            'name' => 'required|max:30',
            'content' => 'required|max:800',
            'sex' => 'in:1,2',
            'phone' => 'required|max:30',
            'code' => ['required',new Code()]
        ]);

        $param['user_id'] = auth('api')->id();
        $model = FirmBrandMessage::query()->create($param);

        if ($model->getKey()){

            if ($param['type']==1){
                event(new ToJoin($param['user_id']));
            }
            return $this->success('留言成功');
        }

        return  $this->error('留言失败');
    }
}
